<?php 
	session_start();
	include('include/db_connect.php');
	include("include/header.php");
	include("include/menu.php"); 
echo '<!-- Start of Main Content Area -->';
echo '<div id="main_content">';

echo '<h1> Winkelwagen </h1><br>';

if(isset($_GET['id']))
{
$id = $_GET['id'];
}
else
{
	$id = 1;
}

if(isset($_GET['action']))
{
	$action = $_GET['action'];
}
else
{
$action = "";
}
switch($action)
{
	case "add": 
	if(isset($_SESSION['cart'][$id]))
	{
		$_SESSION['cart'][$id]++;
	}
	else
	{
		$_SESSION['cart'][$id]=1;
	}
	break;
	case "remove":
	if(isset($_SESSION['cart'][$id]))
	{	$_SESSION['cart'][$id]--;
	if($_SESSION['cart'][$id]==0)
	{
	unset($_SESSION['cart'][$id]);
	}
	}
	break;
	case "empty":
	unset($_SESSION['cart']);
	break;
}
if(isset($_SESSION['cart']))
{
	echo "<table align='center' border =0 cellspacing = 0 cellpadding = 0 width ='640'>";
	$total = 0;
		echo '<tr>';
	echo "<th align='left'>Naam</th>";
	echo "<th align='right'>Aantal</th>";
	echo "<th align='left'> </th>";
	echo "<th align='center'>Prijs </th>";
	echo "<th align='right'>Korting </th>";

	echo "</tr>";

foreach($_SESSION['cart']as $id =>$x)
{
	$result = mysql_query("SELECT naam,prijs,korting FROM PRODUCT WHERE idPRODUCT=$id");
	$myrow = mysql_fetch_assoc($result);
	$name = $myrow['naam'];
	$name = substr($name,0,40);
	$price = $myrow['prijs'];
	$korting = $myrow['korting'];
	$price= $price* ((100-$korting)/100);
	$line_cost = $price * $x;
	$total = $total+$line_cost;
	$line_cost = number_format($line_cost,2,',','');
	$total = number_format($total,2,'.','');
	echo '<tr>';
	echo "<td align='left'> $name </td>";
	echo "<td align='right'>$x</td>";
//	echo "<td align='center'><a href= 'winkelwagen.php?id=".$id."&action=add'>Voeg Toe</a>   <a href= 'winkelwagen.php?id=".$id."&action=remove'>Verwijder</a> </td>";
	echo "<td align='center'><a href='winkelwagen.php?id=".$id."&action=add' TITLE='Voeg 1 aantal toe'><img src='images/shopping_cart_add.jpg' alt='Voeg Product toe'></a>   <a href='winkelwagen.php?id=".$id."&action=remove'TITLE='Verwijder 1 aantal'><img src='images/shopping_cart_remove.jpg' alt='Verwijder Producttoe'></a> </td>";

	echo "<td align='right'> &euro; $line_cost</td>";
	
	echo '<td align="right">'.$korting.'&#37;</td>';
	
	echo "</tr>";
}
echo "<tr>";
echo "<td align='right'><br>Totale prijs:</td>";
echo "<td align='right'><b><br>&euro; $total</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'></td>";
echo "<td align='center'><br><input type='button' value='Ga terug terug naar de vorige pagina.' onClick='history.go(-1)'></td>";
echo "<td align='center'><br><a href= 'bestelling.php' >Klik hier om af te rekenen.</a></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'><br></td>";
echo "</tr>";
echo "</table>";
echo "<table align='center' width='640'>";
echo "<tr>";
#echo '<td></td>';
echo "<td align='center' ><a href= 'winkelwagen.php?action=empty' TITLE='Leeg Winkelwagen'><img src='images/shopping_cart_empty.jpg' alt='Leeg WInkelwagen'></a></td>";
echo "</tr>";
echo "</table>";
}
else	
{
echo "<h2>U heeft geen producten in uw winkelwagen gestopt.</h2>";
}

echo '<h5>&nbsp;</h5>';
echo '</div>';

echo '<div class="clearthis">&nbsp;</div>';?>
<?php include("include/footer.php"); ?>
